package wangjg.android.media.record;

import java.io.File;
import java.io.IOException;
import java.util.List;

import android.hardware.Camera;
import android.hardware.Camera.Size;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;

public class MediaCapture implements Callback {
	private MediaRecorder recorder = new MediaRecorder();
	private SurfaceHolder holder;
	private boolean recording = false;
	private boolean recordAudio = true;
	private boolean recordVideo = true;
	
	public MediaCapture() {
		super();
		this.xx();
	}
	
	private void xx(){
		Camera camera = Camera.open();
		Camera.Parameters params = camera.getParameters();
	    List<Size> supportedPictureSizes = params.getSupportedPictureSizes();
	    List<Size> supportedPreviewSizes = params.getSupportedPreviewSizes();
	    List<Size> supportedVideoSizes = params.getSupportedVideoSizes();
	    List<Integer> supportedPreviewFrameRates = params.getSupportedPreviewFrameRates();
	    List<int[]> supportedPreviewFpsRange = params.getSupportedPreviewFpsRange();
	    
	    this.printSize("supportedPictureSizes",supportedPictureSizes);
	    this.printSize("supportedPreviewSizes",supportedPreviewSizes);
	    this.printSize("supportedVideoSizes",supportedVideoSizes);
	    
	    this.printSize("supportedPreviewFrameRates",supportedPreviewFrameRates);
	    this.printSize("supportedPreviewFpsRange",supportedPreviewFpsRange);
	    
	    camera.release();
	}
	
	private void printSize(String name,List list){
		if(list==null){
			Log.d(getClass().getSimpleName(),name+":"+list);
			return ;
		}
		StringBuilder sb = new StringBuilder();
		for(Object obj:list){
			if(obj instanceof Size){
				Size size = (Size)obj;
				sb.append("width="+size.width+",height="+size.height+",");
			}else if(obj instanceof int[]){
				int[] ia = (int[])obj;
				for(int i:ia){
					sb.append(i+",");
				}
			}else{
				sb.append(obj+",");
			}
		}
		Log.d(getClass().getSimpleName(),name+":" + sb.toString());
	}

	public void start(){
		File file = Environment.getExternalStorageDirectory();
		Log.d("MediaCapture", file.getPath());
		
		recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
		recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
		recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
		recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
		recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
		
		recorder.setVideoFrameRate(5);
		recorder.setVideoSize(320, 240);
		
//		recorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
		
		recorder.setPreviewDisplay(holder.getSurface());
		
		recorder.setOutputFile("/mnt/sdcard/test_av_320x240@5.3gp");
		try {
			recorder.prepare();
		} catch (IOException e) {
			e.printStackTrace();
			return;
		}
		recorder.start();   // Recording is now started
		recording = true;
	}

	public void recordAudio(){
		recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
		recorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
		recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
		recorder.setOutputFile("/mnt/sdcard/test_audio_nb.amr");
		try {
			recorder.prepare();
		} catch (IOException e) {
			e.printStackTrace();
			return;
		}
		recorder.start();
		recording = true;
	}
	
	public void recordVideo(){
		recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
		recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
		recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
		
		recorder.setVideoFrameRate(5);
		recorder.setVideoSize(320, 240);
		
		recorder.setPreviewDisplay(holder.getSurface());
		
		recorder.setOutputFile("/mnt/sdcard/test_video_320x240@5.3gp");
		try {
			recorder.prepare();
		} catch (IOException e) {
			e.printStackTrace();
			return;
		}
		recorder.start();   // Recording is now started
		recording = true;
	}
	
	public void stop(){
		if(!recording)
			return;
		recorder.stop();
		recorder.reset();   // You can reuse the object by going back to setAudioSource() step
		recorder.release(); // Now the object cannot be reused
		recording = false;
	}
	
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
		this.holder = holder;
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		this.holder = holder;
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		this.holder = null;
	}

	public boolean isRecording() {
		return recording;
	}

	public void setRecording(boolean recording) {
		this.recording = recording;
	}

	public boolean isRecordAudio() {
		return recordAudio;
	}

	public void setRecordAudio(boolean recordAudio) {
		this.recordAudio = recordAudio;
	}

	public boolean isRecordVideo() {
		return recordVideo;
	}

	public void setRecordVideo(boolean recordVideo) {
		this.recordVideo = recordVideo;
	}
	
	
}
